package com.nhc.activity_fragment;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.ScrollView;
import android.widget.TextView;

import com.astrob.nhcmap.R;
import com.astrob.util.SysUtils;
import com.nhc.database.manager.QiyaCenter;
import com.nhc.model.SensorSetInfo;
import com.nhc.model.TripDataFileHandle;

import java.text.DecimalFormat;

import zuo.biao.library.base.BaseActivity;
import zuo.biao.library.ui.AlertDialog;
import zuo.biao.library.ui.EditTextInfoWindow;
import zuo.biao.library.ui.TopMenuWindow;

public class SensorSetActivity extends BaseActivity implements View.OnClickListener, AlertDialog.OnDialogButtonClickListener {
    private final String TAG = "SensorSetActivity";

    //启动方法<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

    /**
     * 启动这个Activity的Intent
     *
     * @param context
     * @return
     */
    public static Intent createIntent(Context context) {
        return new Intent(context, SensorSetActivity.class);
    }

    //启动方法>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>


    @Override
    public Activity getActivity() {
        return this;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.sensorset_activity);

        _setInfo = SensorSetInfo.getInstance();
        _setInfo.load(this);
        //功能归类分区方法，必须调用<<<<<<<<<<
        initView();
        initData();
        initEvent();
        //功能归类分区方法，必须调用>>>>>>>>>>

    }

    //UI显示区(操作UI，但不存在数据获取或处理代码，也不存在事件监听代码)<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
    private ScrollView svDemoMain;

    private TextView tv_set3;
    private TextView tv_set4;
    private ImageView iv_useqiyah;

    @Override
    public void initView() {//必须调用
        svDemoMain = (ScrollView) findViewById(R.id.svDemoMain);
        tv_set3 = (TextView) findViewById(R.id.tv_set3);
        tv_set4 = (TextView) findViewById(R.id.tv_set4);

        iv_useqiyah = (ImageView) findViewById(R.id.iv_useqiyah);

    }

    //UI显示区(操作UI，但不存在数据获取或处理代码，也不存在事件监听代码)>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

    //Data数据区(存在数据获取或处理代码，但不存在事件监听代码)<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
    SensorSetInfo _setInfo;

    @Override
    public void initData() {//必须调用
        refresh();

        setUseqiyah(_setInfo.isHeightuse());
    }

    double _wendu = 0;

    void refresh() {

        _wendu = TripDataFileHandle.getInstance().mTripDatainfo.getWendu() + _setInfo.getWenduX();
        String[] WEN = SysUtils.getInstance().getWenduStr(_wendu);
        tv_set3.setText(WEN[0] + WEN[1]);

//        String QIY[] = SysUtils.getInstance().getQiyaStr(_setInfo.getHeight_qiya() * 100);
//        tv_set4.setText(getString(R.string.zdyhpmqy) + " " + QIY[0] + QIY[1]);

        if (QiyaCenter.getInstance()._thisQiyaInfo != null) {
            String QIY[] = SysUtils.getInstance().getQiyaStr(QiyaCenter.getInstance()._thisQiyaInfo.getSPV() * 100);
            tv_set4.setText(QIY[0] + QIY[1]);
        } else {
            tv_set4.setText("");
        }

    }

    //Data数据区(存在数据获取或处理代码，但不存在事件监听代码)>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>


    //Event事件区(只要存在事件监听代码就是)<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
    @Override
    public void initEvent() {//必须调用
        findViewById(R.id.sensorset_3).setOnClickListener(this);
        findViewById(R.id.sensorset_4).setOnClickListener(this);
        findViewById(R.id.sensorset_5).setOnClickListener(this);

    }
    //系统自带监听方法<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
    private int[] CTRL_IDS = {
            R.id.sensorset_3, R.id.sensorset_4, R.id.sensorset_5};

    @Override
    public boolean onKeyUp(int keyCode, KeyEvent event) {
        switch (keyCode) {
            default:
                break;
            case 21://left
            case 22://right
            case 19://up
            case 20://down
            case 23://ok
                setFoucseCtrl();
                break;
        }
        return super.onKeyUp(keyCode, event);
    }

    boolean _isFirst = true;

    private void setFoucseCtrl() {
        if (!_isFirst) {
            return;
        }

        _isFirst = false;

        for (int i = 0; i < CTRL_IDS.length; i++) {
            findViewById(CTRL_IDS[i]).setFocusable(true);
        }
    }

    //类相关监听<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

    @Override
    public void finish() {
        _setInfo.save(this);
        super.finish();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }


    @Override
    public void onForwardClick(View v) {
        //top栏右边按钮响应
        toActivity(TopMenuWindow.createIntent(context, getResources().getStringArray(R.array.top_menu)), REQUEST_TO_TOP_MENU, false);
    }

    boolean _useqiyah = true;

    void setUseqiyah(boolean useqiyah) {
        _useqiyah = useqiyah;
        _setInfo.setHeightuse(useqiyah);
        if (!_useqiyah) {
            iv_useqiyah.setImageResource(R.drawable.swith_close);
        } else {
            iv_useqiyah.setImageResource(R.drawable.swith_open);
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.sensorset_3:
                editWendu();
                break;
            case R.id.sensorset_4:
                if (QiyaCenter.getInstance()._thisQiyaInfo == null) {
                    showShortToast("设备没有气压传感器不能校准。");
                } else {
                    toActivity(QiyaSetActivity.createIntent(this), REQUEST_TO_QIYA);
                }
                break;
            case R.id.sensorset_5:
                setUseqiyah(!_useqiyah);
                break;
            default:
                break;
        }
    }

    private void editWendu() {
        String strUnit = "°C";
        switch (SysUtils.getInstance().wendu_mode) {
            default:
            case 0:
                break;
            case 1:
                strUnit = " °F";
                break;
        }
        intent = EditNumberActivity.createIntentD(context
                , getString(R.string.qdywd), (float)_wendu, strUnit);
        toActivity(intent, REQUEST_TO_WENDU);
    }

    private final int REQUEST_TO_QIYA = 23;
    private final int REQUEST_TO_WENDU = 25;
    private final int REQUEST_TO_TOP_MENU = 30;

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (resultCode != RESULT_OK) {
            return;
        }

        switch (requestCode) {
            case REQUEST_TO_QIYA:
                svDemoMain.smoothScrollTo(0, 0);
                refresh();
                break;
            case REQUEST_TO_WENDU:
                if (data != null) {
                    svDemoMain.smoothScrollTo(0, 0);
                    String editedValue = data.getStringExtra(EditTextInfoWindow.RESULT_VALUE);

                    double wen = -1000;
                    try {
                        wen = Double.valueOf(editedValue);
                    } catch (NumberFormatException e) {

                    }

                    if (wen <= -1000) {
                        showShortToast(getString(R.string.cwdwdz));
                        return;
                    }

                    String strUnit = "°C";
                    switch (SysUtils.getInstance().wendu_mode) {
                        default:
                        case 0:
                            _wendu = wen;
                            break;
                        case 1:
                            strUnit = " °F";
                            _wendu = (wen - 32) / 1.8;
                            break;
                    }
                    tv_set3.setText(new DecimalFormat("0.0").format(wen) + strUnit);
                    _setInfo.setWenduX((float) (_wendu - TripDataFileHandle.getInstance().mTripDatainfo.getWendu()));

                }
                break;
            case REQUEST_TO_TOP_MENU:
                if (data != null) {
                    switch (data.getIntExtra(TopMenuWindow.RESULT_POSITION, -1)) {
                        case 0:
                            _setInfo.reset();
                            refresh();
                            break;
                        default:
                            break;
                    }
                }
                break;
        }
    }


    @Override
    public void onDialogButtonClick(int requestCode, boolean isPositive) {

    }
    //类相关监听>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

    //系统自带监听方法>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>


    //Event事件区(只要存在事件监听代码就是)>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>


}
